//
//  HomeTabBarController.swift
//  WanAndroidLearn
//
//  Created by TRS-Mobile on 2023/11/6.
//

import Foundation
import UIKit
import RAMAnimatedTabBarController
import ReactorKit
import RxTheme


enum HomeTabBarItem: Int {
    case home, login
    
    private func controller(with reactor: some Reactor) -> UIViewController {
        switch self {
        case .home:
            let vc = HomeViewController(reactor: reactor)
            return NavigationController(rootViewController: vc)
        case .login:
            let vc = LoginViewController(reactor: reactor)
            return NavigationController(rootViewController: vc)
        }
    }
    
    var image: UIImage? {
        switch self {
        case .home:
            return R.image.icon_tabbar_home()
        case .login:
            return R.image.icon_tabbar_user()
        }
    }
    
    var title: String {
        switch self {
        case .home:
            return "文章"
        case .login:
            return "我的"
        }
    }
    
    var animation: RAMItemAnimation {
        var ani: RAMItemAnimation
        switch self {
        case .home:
            ani = RAMBounceAnimation()
        case .login:
            ani = RAMRightRotationAnimation()
        }
        ani.theme.iconSelectedColor = themed { $0.secondary }
        ani.theme.textSelectedColor = themed { $0.secondary }
        
        return ani
    }
    
    func getController(with reactor: some Reactor) -> UIViewController {
        let vc = controller(with: reactor)
        let item = RAMAnimatedTabBarItem(title: title, image: image, tag: rawValue)
        item.animation = animation
        item.theme.iconColor = themed { $0.text }
        item.theme.textColor = themed { $0.text }
        vc.tabBarItem = item
        return vc
    }
}

extension ThemeProxy where Base: RAMItemAnimation {
    
    var iconSelectedColor: ThemeAttribute<UIColor> {
        get { fatalError("set only") }
        set {
            base.iconSelectedColor = newValue.value
            let disposable = newValue.stream
                .take(until: base.rx.deallocating)
                .observe(on: MainScheduler.instance)
                .bind(to: base.rx.iconSelectedColor)
            hold(disposable, for: "iconSelectedColor")
        }
    }
    
    var textSelectedColor: ThemeAttribute<UIColor> {
        get { fatalError("set only") }
        set {
            base.textSelectedColor = newValue.value
            let disposable = newValue.stream
                .take(until: base.rx.deallocating)
                .observe(on: MainScheduler.instance)
                .bind(to: base.rx.textSelectedColor)
            hold(disposable, for: "textSelectedColor")
        }
    }
}

extension ThemeProxy where Base: RAMAnimatedTabBarItem {
    var iconColor: ThemeAttribute<UIColor> {
        get { fatalError("set only") }
        set {
            base.iconColor = newValue.value
            let disposable = newValue.stream
                .take(until: base.rx.deallocating)
                .observe(on: MainScheduler.instance)
                .bind(to: base.rx.iconColor)
            hold(disposable, for: "iconColor")
        }
    }
    
    var textColor: ThemeAttribute<UIColor> {
        get { fatalError("set only") }
        set {
            base.textColor = newValue.value
            let disposable = newValue.stream
                .take(until: base.rx.deallocating)
                .observe(on: MainScheduler.instance)
                .bind(to: base.rx.textColor)
            hold(disposable, for: "textColor")
        }
    }
}

class HomeTabBarController: RAMAnimatedTabBarController, View {
    typealias Reactor = HomeTabBarReactor
    
    var disposeBag = DisposeBag()
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    convenience init(reactor: some Reactor) {
        self.init()
        self.reactor = reactor
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
    }
    
    func makeUI() -> Void {
        tabBar.theme.barTintColor = themed { $0.primaryDark }
        themeService.typeStream
            .delay(.milliseconds(100), scheduler: MainScheduler.instance)
            .subscribe(onNext: { theme in
                switch theme  {
                case .light(color: let color), .dark(color: let color):
                    self.changeSelectedColor(color.color, iconSelectedColor: color.color)
                }
            })
            .disposed(by: disposeBag)
    }
    
    func bind(reactor: HomeTabBarReactor) {
        rx.viewWillAppear
            .map { _ in HomeTabBarReactor.Action.refresh }
            .bind(to: reactor.action)
            .disposed(by: disposeBag)
        
        reactor.state
            .map { $0.tabBarItems }
            .filter { $0.count > 0 }
            .delay(.milliseconds(50), scheduler: MainScheduler.instance)
            .asDriver(onErrorJustReturn: [])
            .drive(onNext: { [weak self] (tabBarItems) in
                if let self {
                    let controllers = tabBarItems
                        .map {
                            $0.getController(with: reactor.reactor(for: $0))
                        }
                    self.setViewControllers(controllers, animated: false)
                }
            })
            .disposed(by: disposeBag)
    }
}
